package com.pengpeng.flash.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pengpeng.flash.common.BaseContext;
import com.pengpeng.flash.common.R;
import com.pengpeng.flash.entity.ShoppingCart;
import com.pengpeng.flash.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 新增购物车对象
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart){
        //为当前购物车对象设置当前用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件:数据库中数据的用户id要等于当前登录id
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //判断当前要加入到购物车的是菜品还是套餐
        if (shoppingCart.getDishId() != null){
            //若是菜品则根据菜品id添加查询条件
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //若是套餐则根据套餐id添加查询条件
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //执行查询
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);

        if (serviceOne != null){
            //若查询出对象，说明当前物品已经添加过

            //当前物品数量加一
            serviceOne.setNumber(serviceOne.getNumber() + 1);
            //修改查询出来的对象
            shoppingCartService.updateById(serviceOne);
        }else {
            //若没有查到对象，说明之前没有添加过，就直接赋值保存
            shoppingCart.setCreateTime(LocalDateTime.now());
            serviceOne = shoppingCart;
            shoppingCartService.save(serviceOne);
        }

        return R.success(serviceOne);
    }

    /**
     * 查询购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> cartList(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 减少购物车菜品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> updateCart(@RequestBody ShoppingCart shoppingCart){
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件:数据库中数据的用户id要等于当前登录id
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (shoppingCart.getDishId() != null){
            //若是菜品则根据菜品id添加查询条件
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //若是套餐则根据套餐id添加查询条件
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        if (number == 1){
            shoppingCartService.remove(queryWrapper);
        }else {
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cleanCart(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车清空成功");
    }
}
